package com.dkd.manage.controller;

import com.dkd.common.core.domain.R;
import com.dkd.manage.pojo.dto.ChannelDto;
import com.dkd.manage.pojo.entity.Channel;
import com.dkd.manage.pojo.vo.ChannelVo;
import com.dkd.manage.service.ChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "货道管理相关接口")
@RequestMapping("/manage/channel")
public class ChannelController {

    @Autowired
    private ChannelService channelService;

    @ApiOperation("查询货道列表")
    @PreAuthorize("@ss.hasPermi('manage:channel:list')")
    @GetMapping("/list/{innerCode}")
    public R<List<ChannelVo>> list(@PathVariable String innerCode) {
        log.info("查询货道列表：{}", innerCode);
        List<ChannelVo> channelVoList = channelService.list(innerCode);
        return R.ok(channelVoList);
    }

    @ApiOperation("新增货道")
    @PreAuthorize("@ss.hasPermi('manage:channel:add')")
    @PostMapping
    public R<Integer> add(@RequestBody Channel channel) {
        log.info("新增货道：{}", channel);
        Integer rows = channelService.add(channel);
        return R.ok(rows);
    }

    @ApiOperation("根据id查询货道")
    @PreAuthorize("@ss.hasPermi('manage:channel:query')")
    @GetMapping("/{id}")
    public R<Channel> getChannelById(@PathVariable Long id) {
        log.info("根据id查询货道：{}", id);
        Channel channel = channelService.getChannelById(id);
        return R.ok(channel);
    }

    @ApiOperation("修改货道")
    @PreAuthorize("@ss.hasPermi('manage:channel:edit')")
    @PutMapping
    public R<Integer> edit(@RequestBody Channel channel) {
        log.info("修改货道：{}", channel);
        Integer rows = channelService.edit(channel);
        return R.ok(rows);
    }

    @ApiOperation("根据id删除货道")
    @PreAuthorize("@ss.hasPermi('manage:channel:remove')")
    @DeleteMapping("/{ids}")
    public R<Integer> deleteChannelById(@PathVariable Long[] ids) {
        log.info("根据id删除货道：{}", ids);
        Integer rows = channelService.deleteChannelById(ids);
        return R.ok(rows);
    }

    @ApiOperation("修改设备的货道")
    @PreAuthorize("@ss.hasPermi('manage:channel:edit')")
    @PutMapping("/config")
    public R<Integer> config(@RequestBody ChannelDto channelDto) {
        log.info("修改设备的货道：{}", channelDto);
        Integer rows = channelService.config(channelDto);
        return R.ok(rows);
    }

}
